package com.kitchen.ui.controls
{
	import com.kitchen.model.Vo.AccountVo;
	import com.kitchen.ui.Icon.LeftBoxIcon;
	import com.kitchen.ui.button.CrownButton;
	import com.kitchen.ui.button.DecroButton;
	import com.kitchen.ui.button.DiaryButton;
	import com.kitchen.ui.button.FoodButton;
	import com.kitchen.ui.button.LeftinButton;
	import com.kitchen.ui.button.ShopButton;
	import com.kitchen.ui.button.ToolButton;
	
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.utils.Timer;

	public class LeftToolBar extends Sprite
	{
		public static const S_DIARY:String = 'sdiary';
		public static const S_SHOP:String = 'sshop';
		public static const S_TOOL:String = 'stool';
		public static const S_FOOD:String = 'sfood';
		public static const S_DECORATE:String = 'sdecorate';
		public static const S_CROWN:String = 'scrown';
		private var account:AccountVo;
		private var skin:BitmapData;
		private var leftinButton:LeftinButton;
		private var state:int;
		private var timer:Timer;
		
		public function LeftToolBar(skin:BitmapData)
		{
			super();
			this.skin = skin;
			this.state = 1;
			this.y = 150;
			this.timer = new Timer(50);
			this.timer.addEventListener(TimerEvent.TIMER, onTimer);
			this.layout();
		}
		
		private function layout():void{
			var background:LeftBoxIcon = new LeftBoxIcon(this.skin);
			this.addChild(background);
			var diaryButton:DiaryButton = new DiaryButton(this.skin);
			diaryButton.name = LeftToolBar.S_DIARY;
			diaryButton.x = 6;
			diaryButton.y = 7;
			diaryButton.addEventListener(MouseEvent.CLICK, onClickHandle);
			this.addChild(diaryButton);
			var toolButton:ToolButton = new ToolButton(this.skin);
			toolButton.name = LeftToolBar.S_TOOL;
			toolButton.x = 6;
			toolButton.y = 65;
			toolButton.addEventListener(MouseEvent.CLICK,onClickHandle);
			this.addChild(toolButton);
			var shopButton:ShopButton = new ShopButton(this.skin);
			shopButton.name = LeftToolBar.S_SHOP;
			shopButton.x = 6;
			shopButton.y = 123;
			shopButton.addEventListener(MouseEvent.CLICK, onClickHandle);
			this.addChild(shopButton);
			var foodButton:FoodButton = new FoodButton(this.skin);
			foodButton.name = LeftToolBar.S_FOOD;
			foodButton.x = 6;
			foodButton.y = 182;
			foodButton.addEventListener(MouseEvent.CLICK, onClickHandle);
			this.addChild(foodButton);
			var decroButton:DecroButton = new DecroButton(this.skin);
			decroButton.name = LeftToolBar.S_DECORATE;
			decroButton.x = 6;
			decroButton.y = 240;
			decroButton.addEventListener(MouseEvent.CLICK, onClickHandle);
			this.addChild(decroButton);
			var crownButton:CrownButton = new CrownButton(this.skin);
			crownButton.name = LeftToolBar.S_CROWN;
			crownButton.x = 6;
			crownButton.y = 298;
			crownButton.addEventListener(MouseEvent.CLICK, onClickHandle);
			this.addChild(crownButton);
			crownButton.addEventListener(MouseEvent.CLICK, onClickHandle);
			leftinButton = new LeftinButton(this.skin);
			leftinButton.name = 'leftin';
			leftinButton.x = 100;
			leftinButton.y = 160;
			this.addChild(leftinButton);
			leftinButton.addEventListener(MouseEvent.CLICK, onClickHandle);
		}
		
		private function onClickHandle(me:MouseEvent):void{
			var cmd:String = me.target.name;
			if(cmd == 'leftin'){
				this.state*=-1;
				if(this.state == 1){
					this.leftinButton.rotation = 0;
				}else if(this.state == -1){
					this.leftinButton.rotation = 180;
				}
				this.timer.start();
			}else{
				this.dispatchEvent(new Event(cmd));
			}
		}
		
		private function onTimer(te:TimerEvent):void{
			//trace('t');
			if(this.state == 1){
				if(Math.abs(this.x-0) > 30){
					if(this.x < 0){
						this.x += 30;
					}
				}else{
					this.x = 0;
				}
				if(this.x == 0){
					this.timer.stop();
				}
			}else if(this.state == -1){
				if((this.x + 80) > 30){
					if(this.x > -80){
						this.x -= 30;
					}
				}else{
					this.x = -80;
				}
				if(this.x == -80){
					this.timer.stop();
				}
			}
		}
	}
}